import datetime
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdrivermanager_cn.chrome import ChromeDriverManager

# 一、练习打开浏览器
# # 打开浏览器----驱动管理
# driver_path = ChromeDriverManager().install()
#
# # 1.创建谷歌浏览器驱动对象
# driver = webdriver.Chrome(service=Service(driver_path))
#
# # 2. 输入bing网址“https://cn.bing.com/”
# driver.get("https://cn.bing.com/")
# time.sleep(2)
#
# # 3.找到bing输入框，输入“自动化测试宝典”
# driver.find_element(By.CSS_SELECTOR, "#sb_form_q").send_keys("自动化测试")
# time.sleep(2)
#
# # 4.点击一下搜狗搜索按钮
# driver.find_element(By.CSS_SELECTOR, "#search_icon > svg").click()
# time.sleep(2)
#
# # 5.关闭谷歌浏览器
# driver.quit()

# 二、元素定位
driver_path = ChromeDriverManager().install()

options = webdriver.ChromeOptions()
# 添加无头模式
# options.add_argument("-headless")

options.page_load_strategy = 'normal'   # 等待所有资源加载完成
# options.page_load_strategy = 'eager'    # DOM访问就绪就开始
# options.page_load_strategy = 'none'     # 完全不阻塞，直接继续执行

driver = webdriver.Chrome(service=Service(driver_path), options=options)

# driver.get("https://www.baidu.com/")
# time.sleep(4)

# time.sleep(3)
# driver.fullscreen_window()

# print(driver.title)     # 打印该网页标题

# ret = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# ret = driver.find_elements(By.CSS_SELECTOR, "#s-top-left > a")
# for i in ret:
#     print(i.text)   # 获取每个元素对应的文本信息

# driver.quit()

# driver.get("https://www.sogou.com")
# time.sleep(3)
#
# ret = driver.find_elements(By.CSS_SELECTOR, "#wrap > div.header > div.top-nav > ul")
# for x in ret:
#     print(x.text)
#
# driver.quit()

# 三、点击事件
# driver.get("https://www.sogou.com/")
# driver.find_element(By.CSS_SELECTOR, "#query").send_keys("2024巴黎奥运会")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#stb").click()
#
# # 四、获取页面信息
# # 获取文本信息
# text = driver.find_element(By.CSS_SELECTOR, "#cur-weather > span:nth-child(1)").text
# print(text)
# assert text == "河南 洛阳"
#
# # 获取搜狗搜索按钮上的文字
# text1 = driver.find_element(By.CSS_SELECTOR, "#stb").get_attribute("value")
# print(text1)
# assert text1 == "搜狗搜索"
#
# # 获取页面标题和url
# print(driver.title)
# print(driver.current_url)
# time.sleep(2)
#
# driver.back()
# driver.find_element(By.CSS_SELECTOR, "#query").clear()
# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR, "#pic").click()
#
# # 获取当前页面的句柄---第一个标签页
# curHandle = driver.current_window_handle
#
# # 获取所有句柄
# allHandle = driver.window_handles
# # 遍历所有的句柄，切换到新的界面
# for handle in allHandle:
#     if handle != curHandle:
#         # 切换句柄
#         driver.switch_to.window(handle)
#
# # 测试跳转结果
# print("after:"+driver.title)
# print("after:"+driver.current_url)
#
# driver.quit()

# 五、设置窗口大小
# # 最大化
# driver.maximize_window()
# time.sleep(2)
#
# # 最小化
# driver.minimize_window()
# time.sleep(2)
#
# # 全屏
# driver.fullscreen_window()
# time.sleep(2)
#
# # 手动设置尺寸
# driver.set_window_size(900,800)
# time.sleep(2)
#
# driver.quit()

# 六、添加屏幕截图
# driver.get("https://baidu.com/")
# filename = "./test.png"
# driver.save_screenshot(filename)

# 每次生成的图片文件都不一样
# driver.get("https://www.bilibili.com/")
# driver.find_element(By.CSS_SELECTOR, "#nav-searchform > div.nav-search-content > input").send_keys("听风的蚕")
# driver.find_element(By.CSS_SELECTOR, "#nav-searchform > div.nav-search-btn > svg").click()
#
# curHandle = driver.current_window_handle
#
# # 获取所有句柄
# allHandle = driver.window_handles
# for cur in allHandle:
#     if cur != curHandle:
#         driver.switch_to.window(cur)
#
# print("after: " + driver.title)
# driver.close()
#
# time.sleep(3)
#
# # autotest-2024/8/15-1813.png
# filename = "autoTest-" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png"
# # ./images/autotest-2024/8/15-1813.png
# driver.save_screenshot("./images/" + filename)
#
# driver.quit()

# 七、警告弹窗
# driver.get("file:///D:/Professional_software/VS/Code/test-learning/AutoTest01/selenium-html/alert.html")
# time.sleep(2)
#
# driver.find_element(By.CSS_SELECTOR, "#tooltip").click()
# # 切换到弹窗
# alter = driver.switch_to.alert
# time.sleep(2)
# # 点击弹窗的确定按钮
# # alter.accept()
# driver.quit()

# 点击弹窗的取消按钮
# driver.get("file:///D:/Professional_software/VS/Code/test-learning/AutoTest01/selenium-html/confirm.html")
# driver.find_element(By.CSS_SELECTOR, "body > input[type=button]").click()
# time.sleep(3)
#
# # 切换到弹窗
# alter = driver.switch_to.alert
# 点取消按钮
# alter.dismiss()

# 提示弹窗---输入框
# driver.get("file:///D:/Professional_software/VS/Code/test-learning/AutoTest01/selenium-html/Prompt.html")
# driver.find_element(By.CSS_SELECTOR, "body > input[type=button]").click()
#
# # #输入文本+确定+取消
# alert = driver.switch_to.alert
# # 自动化输入的内容肉眼不可见
# alert.send_keys("自动化测试 [弹窗信息]")
# time.sleep(2)
#
# # 点击确定
# alert.accept()
# # 点击取消
# # alert.dismiss()
# time.sleep(1)
#
# driver.save_screenshot("./images/" + "弹窗输入框结果.png")
# driver.quit()

# 八、等待
# 1. 添加隐式等待
# driver.get("https://www.sogou.com/")
# 作用于全局
# driver.implicitly_wait(1)
#
# text = driver.find_element(By.CSS_SELECTOR, "#stb").get_attribute("value")
# print(text)
#
# driver.quit()

# 2. 添加显式等待
# driver.get("https://www.baidu.com/")
#
# textValue = driver.find_element(By.CSS_SELECTOR, "#hotsearch-refresh-btn > span").text
# print(textValue)
#
# # 显式等待功能还有很多
# wait = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.CSS_SELECTOR,  "#s-hotsearch-wrapper > div > a.hot-title > div > i:nth-child(1)")))
#
# # 迭代打印热榜
# texts = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# for j in texts:
#     print(j.text)
#
# driver.quit()

# 显式等待弹窗是否就绪
# driver.get("file:///D:/Professional_software/VS/Code/test-learning/AutoTest01/selenium-html/alert.html")
#
# driver.find_element(By.CSS_SELECTOR, "#tooltip").click()
#
# wait = WebDriverWait(driver, 3)
# # 等到弹窗是否出现---> (alert_is_present)
# wait.until(EC.alert_is_present())
# # 弹窗同意
# driver.switch_to.alert.accept()
#
# driver.quit()
#  # 显式等待和隐式等待不建议一起使用

# 九、back、forward、refresh
# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("Pycharm")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# time.sleep(2)
# # 后退：回到百度首页
# driver.back()
#
# time.sleep(2)
# # 前进：回到Pycharm关键词页面
# driver.forward()
#
# time.sleep(2)
# # 刷新
# driver.refresh()
#
# time.sleep(2)

# 十、上传文件
# driver.get("file:///D:/Professional_software/VS/Code/test-learning/AutoTest01/selenium-html/upload.html")
# # 模拟选中文件的一个过程
# driver.find_element(By.CSS_SELECTOR, "body > div > div > input[type=file]").send_keys("D:\\Professional_software\\VS\\Code\\test-learning\\AutoTest01\\qodana.yaml")
# time.sleep(2)

# driver.save_screenshot("./images/" + "上传文件.png")
# time.sleep(3)

# driver.quit()
